package MainTest;

/**
 * Created by xuyi on 2017/2/20.
 */
public class FindGreatestSumOfSubArray {

    public static boolean is(String str) {
        str = "min";
        System.out.println(str);
        return str.toLowerCase() == "ad";
    }

    public static void main(String[] args) {
        String a = "ad";

        boolean b = is(a);
        System.out.println(b);

        System.out.println(a);


//        int[] arr = {6,-3,-2,7,-15,1,2,2};
//        FindGreatestSumOfSubArray subArray = new FindGreatestSumOfSubArray();
//        int i = subArray.FindGreatestSumOfSubArray(arr);
//        System.out.println(i);
    }

    public int FindGreatestSumOfSubArray(int[] array) {
        int max = array[0];
        Integer temp = null;

        int[] tempArr = new int[array.length];
        int tempIdx = 0;

        for (int i = 0; i < array.length; i++) {
            int ai = array[i];
            if (ai > max) {
                max = ai;
            }
            if (temp == null) {
                temp = ai;
            } else if (temp >= 0) {
                if (ai >= 0) {
                    temp += ai;
                } else if (ai < 0) {
                    tempArr[tempIdx] = temp;
                    temp = ai;
                    tempIdx++;
                }
            } else if (temp < 0) {
                if (ai < 0) {
                    temp += ai;
                } else if (ai >= 0) {
                    tempArr[tempIdx] = temp;
                    temp = ai;
                    tempIdx++;
                }
            }
        }
            tempArr[tempIdx] = temp;
        Integer couple = null;
        int resultIdx = 0;
        int[] resultArr = new int[tempArr.length];

        for (int i = 0; i < tempArr.length; i++) {
            int ti = tempArr[i];
            if (couple == null) {
                if (ti >= 0) {
                    couple = ti;
                }
            } else {
                couple += ti;
                if (ti < 0) {
                    resultArr[resultIdx] = couple;
                    couple = null;
                    resultIdx++;
                }
            }
        }
        if (couple != null) {
            resultArr[resultIdx] = couple;
        }
        int result = resultArr[0];
        int tempResult = 0;
        for (int i = 0; i < resultArr.length; i++) {
            int ri = resultArr[i];
            if (ri >= 0) {
                tempResult += ri;
            } else {
                if (tempResult > result) {
                    result = tempResult;
                }
                tempResult = 0;
            }
        }
        return result;
    }




}
